﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;

namespace iLangl.Customer.DAL.XMLStorage
{
	public class TranslationDAL : BaseStorageDAL, ITranslationDAL
	{
		#region ITranslationDAL Members

		public void CreateTranslation(Model.Translation translation, System.Data.IDbTransaction transaction)
		{
			dtoXmlStorage.TranslationRow translationRow = Storage.Translation.FindById(translation.Id);

			bool isNew = (translationRow == null);

			if (isNew)
			{
				translationRow = Storage.Translation.NewTranslationRow();
			}

			translationRow.Id = translation.Id;
			translationRow.Progress = translation.Progress;
			translationRow.Comment = translation.Comment;
			translationRow.CustomerIdentity = translation.CustomerIdentity;
			translationRow.TPIdentity = translation.TPIdentity;
			translationRow.Status = translation.Status;

			if (translation.Responsible != null)
				translationRow.ResponsibleId = translation.Responsible.Id;
		
			if(translation.Project != null)
				translationRow.ProjectId = translation.Project.Id;
			

			if (translation.LanguagePair != null)
			{
				if(translation.LanguagePair.SourceLanguage != null)
					translationRow.SourceLanguageCode = translation.LanguagePair.SourceLanguage.LanguageId;

				if (translation.LanguagePair.TargetLanguage != null)
					translationRow.TargetLanguageCode = translation.LanguagePair.TargetLanguage.LanguageId;
			}

			if (translation.SourceContent != null)
			{
				translationRow.SourceContentId = translation.SourceContent.Id;
			}

			if (translation.TargetContent != null)
			{
				translationRow.TargetContentId = translation.TargetContent.Id;
			}

			if (isNew)
			{
				Storage.Translation.AddTranslationRow(translationRow);
			}


			if (transaction == null)
			{
				AcceptChanges();
			}
		}

		#endregion


		public Translation ReadTranslation(string tranlationId)
		{
			dtoXmlStorage.TranslationRow translationRow = Storage.Translation.FindById(tranlationId);
			Translation translation = new Translation();
			translation.Id = translationRow.Id;
			translation.Progress = translationRow.Progress;

			translation.Project = new Project();
			translation.Project.Id = translationRow.ProjectId;

			if (!translationRow.IsStatusNull())
				translation.Status = translationRow.Status;

			if (!translationRow.IsCommentNull())
				translation.Comment = translationRow.Comment;

			if (!translationRow.IsCustomerIdentityNull())
				translation.CustomerIdentity = translationRow.CustomerIdentity;

			if (!translationRow.IsTPIdentityNull())
				translationRow.TPIdentity = translationRow.TPIdentity;

			LanguagePair lp = new LanguagePair();
			lp.SourceLanguage = new Language(translationRow.SourceLanguageCode, string.Empty);
			lp.TargetLanguage = new Language(translationRow.TargetLanguageCode, string.Empty);
			translation.LanguagePair = lp;

			return translation;
		}

		public IList<Translation> ReadTranslationsByProject(Project project)
		{
			List<Translation> result = new List<Translation>();

			foreach (dtoXmlStorage.TranslationRow translationRow in Storage.Translation)
			{
				if (string.Compare(translationRow.ProjectId, project.Id, true) == 0)
				{

					Translation translation = ReadTranslation(translationRow.Id);

					if (!translationRow.IsSourceContentIdNull())
						translation.SourceContent = project.FindContent(translationRow.SourceContentId);
					if (!translationRow.IsTargetContentIdNull())
						translation.TargetContent = project.FindContent(translationRow.TargetContentId);

					translation.Project = project;
					

					result.Add(translation);
				}
			}

			return result;
		}



		#region ITranslationDAL Members


		public Translation[] ReadTranslations(string sourceCustomerIdentity, string targetCustomerIdentity, string sourceLanguageCode, string targetLanguageCode)
		{
			List<Translation> result = new List<Translation>();

			foreach (dtoXmlStorage.TranslationRow tr in Storage.Translation)
			{
				if (tr.SourceLanguageCode != sourceLanguageCode)
					continue;

				if (tr.TargetLanguageCode != targetLanguageCode)
					continue;


				if (!tr.IsSourceContentIdNull())
				{
					dtoXmlStorage.ContentRow cr = Storage.Content.FindById(tr.SourceContentId);
					if (cr.CustomerIdentity != sourceCustomerIdentity)
						continue;
				}

				if (!tr.IsTargetContentIdNull())
				{
					dtoXmlStorage.ContentRow cr = Storage.Content.FindById(tr.TargetContentId);
					if (cr.CustomerIdentity != targetCustomerIdentity)
						continue;
				}

				result.Add(ReadTranslation(tr.Id));
			}
				
			return result.ToArray();
		}


		#endregion
	}
}
